﻿using Glue;
using Glue.Converters;
using Xunit;

namespace GlueTests.MappingTests
{
    public class When_adding_a_relation_together_with_converter
    {
        private readonly Mapping<DomainThing, DtoThing> mapping;
        private readonly IConverter converter;

        public class DomainThing
        {
            public bool ShouldBe { get; set; }
        }

        public class DtoThing
        {
            public bool ShouldNotBe { get; set; }
        }
        public When_adding_a_relation_together_with_converter()
        {
            mapping = new Mapping<DomainThing,DtoThing>();
            converter = Converting.ToNegatedBoolean();
        }

        [Fact]
        public void Should_be_same_converter_as_the_one_added_when_relating_TwoWays()
        {
            mapping.Relate(domain => domain.ShouldBe, dto => dto.ShouldNotBe, converter);
            AssertSameConverter();
        }

        [Fact]
        public void Should_be_same_converter_as_the_one_added_when_relating_TowardsLeft()
        {
            mapping.RelateTowardsLeft(domain => domain.ShouldBe, dto => dto.ShouldNotBe, converter);
            AssertSameConverter();
        }

        [Fact]
        public void Should_be_same_converter_as_the_one_added_when_relating_TowardsRight()
        {
            mapping.RelateTowardsRight(domain => domain.ShouldBe, dto => dto.ShouldNotBe, converter);
            AssertSameConverter();
        }

        private void AssertSameConverter()
        {
            var relation = mapping.Relations.Get(domain => domain.ShouldBe, dto => dto.ShouldNotBe);
            Assert.Same(converter, relation.GetConverter());
        }
    }
}